package com.sc.es.controller.backend;


import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.sc.es.common.ApiResponse;
import com.sc.es.common.HouseResult;
import com.sc.es.common.ServiceResult;
import com.sc.es.dto.*;
import com.sc.es.entity.SupportAddress;
import com.sc.es.search.ISearchService;
import com.sc.es.service.IAddressService;
import com.sc.es.service.IHouseService;
import com.sc.es.service.IQiNiuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;


/**
 * @author sc
 * Created on  2018/2/6
 */
@Controller
@RequestMapping("/admin")
public class AdminViewController {
    @Autowired
    private IHouseService houseService;
    @Autowired
    private IAddressService addressService;


    @GetMapping("/center")
    public String adminCenterPage(){
        return "admin/center";
    }
    @GetMapping("welcome")
    public String welcome(){
        return "admin/welcome";
    }
    @GetMapping("/login")
    public String loginPage(){
        return "admin/login";
    }
    @GetMapping("/house-add")
    public String addHouse(){
        return "admin/house-add";
    }
    @GetMapping("/house/list")
    public String listHouse(){
        return "admin/house-list";
    }


    @GetMapping("/house/edit")
    public String houseEditPage(@RequestParam("id")Integer id, Model model){
        HouseResult completeOne = houseService.findCompleteOne(id);
        if(!completeOne.isSuccess()){
            return "404";
        }
        HouseDto result = (HouseDto) completeOne.getResult();
        model.addAttribute("house",completeOne);
        Map<SupportAddress.Level, SupportAddressDto> cityAndRegion = addressService.findCityAndRegion(result.getCityEnName(), result.getRegionEnName());
        model.addAttribute("city",cityAndRegion.get(SupportAddress.Level.CITY));
        model.addAttribute("region",cityAndRegion.get(SupportAddress.Level.REGION));
        HouseDetailDto houseDetail = result.getHouseDetail();
        HouseResult<SubwayDTO> subwayRes = addressService.findSubWayById(houseDetail.getSubwayLineId());
        if(subwayRes.isSuccess()){
            model.addAttribute("subway",subwayRes.getResult());
        }
        HouseResult<SubwayStationDTO> subWayStation = addressService.findSubWayStationById(houseDetail.getSubwayStationId());
        if(subWayStation.isSuccess()){
            model.addAttribute("station",subWayStation.getResult());
        }
        return "admin/house-edit";
    }


}
